package cn.xd.base.controller.admin;

import cn.hutool.core.util.ObjUtil;
import cn.xd.base.application.dto.cmd.CheckNameDTO;
import cn.xd.base.application.dto.query.AdminOrgRoleQuery;
import cn.xd.base.application.dto.query.AdminRoleQuery;
import cn.xd.base.application.service.command.AdminRoleAppService;
import cn.xd.base.application.service.query.AdminOrgQueryAppService;
import cn.xd.base.application.service.query.AdminRoleQueryAppService;
import cn.xd.base.domain.bean.command.AddAdminRoleCommand;
import cn.xd.base.domain.bean.command.UpdateAdminRoleCommand;
import cn.xd.base.domain.bean.command.common.ChangeStatusCommand;
import cn.xd.base.domain.bean.response.AdminOrgRoleDTO;
import cn.xd.base.domain.bean.response.AdminRoleDTO;
import cn.xd.base.domain.bean.response.AdminRoleDetailDTO;
import cn.xd.common.response.ResponseResult;
import cn.xd.web.utils.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理（adminRole）
 *
 * @author mirzalin
 */
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {
    @Resource
    private AdminRoleAppService adminRoleAppService;
    @Resource
    private AdminRoleQueryAppService adminRoleQueryAppService;

    @Resource
    private AdminOrgQueryAppService adminOrgQueryAppService;

    /**
     * 检查角色名称是否存在
     *
     * @param name
     * @return
     */
    @PostMapping("/checkHasName")
    public ResponseResult<Boolean> checkHasName(@Validated @RequestBody CheckNameDTO dto) {
        return ResponseResult.success(adminRoleQueryAppService.checkHasName(dto.getName(), dto.getId()));
    }

    /**
     * 新增
     *
     * @param command
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<?> add(@Validated @RequestBody AddAdminRoleCommand command) {
        adminRoleAppService.addAdminRole(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 更新
     *
     * @param command
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<?> update(@Valid @RequestBody UpdateAdminRoleCommand command) {
        List<Long> orgIds = adminOrgQueryAppService.getAdminOrgChildrenIds(SecurityUtils.getAdminUserOrgId());
        adminRoleAppService.updateAdminRole(command, orgIds, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 更新状态
     *
     * @param command
     * @return
     */
    @PostMapping("/change/Status")
    public ResponseResult<?> changeAdminRoleStatus(@Valid @RequestBody ChangeStatusCommand command) {
        adminRoleAppService.changeAdminRoleStatus(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 删除
     *
     * @param id 删除的角色id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseResult<?> delete(@PathVariable Long id) {
        List<Long> orgIds = adminOrgQueryAppService.getAdminOrgChildrenIds(SecurityUtils.getAdminUserOrgId());
        adminRoleAppService.delete(id, orgIds);
        return ResponseResult.success();
    }

    /**
     * 查询角色明细
     *
     * @param id
     * @return
     */
    @PostMapping("/info/{id}")
    public ResponseResult<AdminRoleDetailDTO> getInfo(@PathVariable Long id) {
        return ResponseResult.success(adminRoleQueryAppService.getInfo(id));
    }

    /**
     * 查询角色（分页）
     *
     * @param query
     * @return
     */
    @PostMapping("/page")
    public ResponseResult<Page<AdminRoleDTO>> getPage(@RequestBody AdminRoleQuery query) {
        if (ObjUtil.isEmpty(SecurityUtils.getAdminUserOrgId())) {
            return ResponseResult.success(new Page<>());
        }
        return ResponseResult.success(adminRoleQueryAppService.getPage(query, adminOrgQueryAppService.getAdminOrgChildrenIds(SecurityUtils.getAdminUserOrgId())));
    }

    /**
     * 查询角色（不分页）
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<AdminRoleDTO>> getList(@RequestBody AdminRoleQuery query) {
        return ResponseResult.success(adminRoleQueryAppService.getList(query, adminOrgQueryAppService.getAdminOrgChildrenIds(SecurityUtils.getAdminUserOrgId())));
    }

    /**
     * 机构角色分页
     *
     * @param query
     * @return
     */
    @PostMapping("/getAdminOrgRolePage")
    public ResponseResult<Page<AdminOrgRoleDTO>> getAdminOrgRolePage(@RequestBody AdminOrgRoleQuery query) {
        return ResponseResult.success(adminRoleQueryAppService.getAdminOrgRolePage(query, adminOrgQueryAppService.getAdminOrgChildrenIds(SecurityUtils.getAdminUserOrgId())));
    }
}
